﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ServerPlatform.Web;
using ServerPlatform.Exceptions;
using MapEntities;

namespace WebApp.Controls
{

    public enum EntityMode
    {
        Default,
        Shape,
        Symbol

    }
    public partial class LayerItemBindingRuleEditor : BaseDataEditor<LayerItemBindingRule>
    {
        public EntityMode Mode { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                foreach (string xlayerinfo in Enum.GetNames(typeof(LayerInfo)))
                {
                    ctlLayerInfo.Items.Add(new ListItem(xlayerinfo));
                }
            }
        }





        public override void Edit(LayerItemBindingRule item)
        {
            ctlListingRule.Edit(item.ListingRule);
            ctlItemRule.Edit(item.BindingRule);
            base.Edit(item);
        }

        public override LayerItemBindingRule EndEdit()
        {
            LayerItemBindingRule result = base.EndEdit();
            return result;
        }

        protected override LayerItemBindingRule CreateDefaultInstance()
        {
            if (Mode == EntityMode.Default)
                return base.CreateDefaultInstance();
            else if (Mode == EntityMode.Shape)
                return new ShapeItemBindingRule();
            else return new SymbolItemBindingRule();
        }

        protected override void ShowEntity(LayerItemBindingRule entity)
        {
            if (entity.ID != null && entity.ID != Guid.Empty)
            {
                ctlTextBinding.Text = entity.TextBinding;
                ctlTooltipBinding.Text = entity.TooltipBinding;
                ctlLayerInfo.SelectedIndex = ctlLayerInfo.Items.IndexOf(ctlLayerInfo.Items.FindByValue(entity.ViewLayer.ToString()));
                ctlCustomLayerName.Text = entity.CustomLayer;
                ctlItemRule.Edit(entity.BindingRule);
                ctlListingRule.Edit(entity.ListingRule);
            }
            else
            {
                ctlTextBinding.Text = string.Empty;
                ctlTooltipBinding.Text = string.Empty;
                ctlLayerInfo.SelectedIndex = 0;
                ctlCustomLayerName.Text = string.Empty;
            }

        }

        public override LayerItemBindingRule GetEntity()
        {
            LayerItemBindingRule result = Current;
            result.ListingRule = ctlListingRule.EndEdit();
            result.BindingRule = ctlItemRule.EndEdit();
            result.TextBinding = ctlTextBinding.Text;
            result.TooltipBinding = ctlTooltipBinding.Text;
            result.ViewLayer = (LayerInfo)ctlLayerInfo.SelectedIndex;
            if (string.IsNullOrEmpty(ctlCustomLayerName.Text.Trim()))
            {
                BusinessException ee = new BusinessException("Custom Layer Name cannot be empty");
                throw ee;
            }
            else result.CustomLayer = ctlCustomLayerName.Text.Trim();
            return result;
        }
    }
}